package com.pkx.guide.qianfeng.service.impl;

import com.pkx.guide.qianfeng.service.QianFengSocketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;

@Service
@Slf4j
public class QianFengSocketServiceImpl implements QianFengSocketService {

    /**
     * 创建连接
     * @param host
     * @param port
     * @return
     */
    @Override
    public Socket create(String host, Integer port) throws IOException {
        Socket socket = new Socket();
        SocketAddress endpoint = new InetSocketAddress(host, port);
        log.info("ip={}----port={}", host, port);
        try {
            socket.connect(endpoint, 3000);
        } catch (IOException e) {
            log.error("创建连接失败，{}", e.getMessage());
            socket.close();
            e.printStackTrace();
        }
        return socket;
    }

    /**
     * 发送
     * @param bytes
     * @param socket
     */
    @Override
    public void send(byte[] bytes,Socket socket ){
        log.info("发送数据开始：{}");
        try {
            DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
            log.error("发送的信息:{}"+outputStream);
            outputStream.write(bytes);
            outputStream.flush();
            log.error("写入的数据：{}"+bytes);
        } catch (SocketTimeoutException s){
            log.error("Socket发送超时，{}", s.getMessage());
            s.printStackTrace();
        } catch (Exception e) {
            log.error("Socket发送失败，{}", e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 回执
     * @param socket
     * @return
     */
    @Override
    public byte[] receive(Socket socket){
        try {
            socket.setSoTimeout(1500);
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
             log.error("aaaa"+socket.getInputStream());

             log.error("接收到的数据：{}"+inputStream);
            byte[] bytes = new byte[8192];
            int read = inputStream.read(bytes);
            if (read > 0) {
                // 创建一个恰好大小的字节数组来存储实际接收到的数据
                byte[] actualData = new byte[read];
                System.arraycopy(bytes, 0, actualData, 0, read);
                log.error("接收到的数据长度：{}"+read);
                return actualData;
            }
            log.error("接收到的数据：{}"+read);
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public byte[] receives(Socket socket) throws IOException {
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            byte[] bytes = new byte[8192];
            inputStream.read(bytes);
            return bytes;

    }

    @Override
    public Socket creates(String ip, Integer port) {
        Socket clientSocket = null;
        try {
            // 使用InetSocketAddress创建连接地址
            InetSocketAddress endpoint = new InetSocketAddress(ip, port);
            // 创建Socket并设置连接超时
            clientSocket = new Socket();
            clientSocket.connect(endpoint, 1500);
            return clientSocket;
        } catch (IOException e) {
            log.error("创建连接失败，{}", e.getMessage());
            if (clientSocket != null) {
                try {
                    clientSocket.close();
                } catch (IOException closeException) {
                    log.error("关闭Socket失败，{}", closeException.getMessage());
                }
            }
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Socket createTcps(String ip, Integer port)  {
        // 创建TCP客户端并连接到服务器
        String serverAddress = "192.168.0.115";  // 修正：使用传入的ip参数
        int serverPort = port;
        Socket clientSocket = new Socket();  // 修正：使用无参构造函数
        log.info("ip={}----port={}", serverAddress, "5227");

        InetSocketAddress endpoint = new InetSocketAddress("localhost", 5227);
        try {
            clientSocket.connect(endpoint, 3);
        } catch (IOException e) {
            log.error("创建连接失败，{}", e.getMessage());
            if (clientSocket != null) {
                try {
                    clientSocket.close();
                } catch (IOException closeException) {
                    log.error("关闭Socket失败，{}", closeException.getMessage());
                }
            }
        }
        return clientSocket;
    }


}
